<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" th:replace="~{fragments/layout :: layout(~{::title}, ~{::section})}">

<head>
    <title>新增借阅 - 图书馆管理系统</title>
</head>

<body>
    <section>
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h2>新增借阅</h2>
            <a href="/borrow/list" class="btn btn-secondary">
                <i class="fas fa-arrow-left"></i> 返回列表
            </a>
        </div>

        <div class="card">
            <div class="card-body">
                <form id="addBorrowForm">
                    <div class="form-group">
                        <label for="userId">借阅用户</label>
                        <select class="form-control" id="userId" name="userId" required>
                            <option value="">请选择用户</option>
                            <option th:each="user : ${users}" th:value="${user.id}"
                                th:text="${user.name + ' (' + user.username + ')'}">用户名</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="bookId">图书</label>
                        <select class="form-control" id="bookId" name="bookId" required>
                            <option value="">请选择图书</option>
                            <option th:each="book : ${books}" th:value="${book.id}" th:text="${book.name}">图书名称</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="borrowTime">借阅时间</label>
                        <input type="datetime-local" class="form-control" id="borrowTime" name="borrowTime" required>
                    </div>
                    <div class="form-group">
                        <label for="expectedReturnTime">应还时间</label>
                        <input type="datetime-local" class="form-control" id="expectedReturnTime" name="expectedReturnTime" required>
                    </div>
                    <div class="form-group">
                        <button type="submit" class="btn btn-primary">
                            <i class="fas fa-save"></i> 保存
                        </button>
                    </div>
                </form>
            </div>
        </div>

        <script>
            $(document).ready(function () {
                // 设置默认的借阅时间和应还时间
                const now = new Date();
                const dueDate = new Date();
                dueDate.setDate(dueDate.getDate() + 30); // 默认借阅30天

                $('#borrowTime').val(formatDateTime(now));
                $('#expectedReturnTime').val(formatDateTime(dueDate));

                $('#addBorrowForm').submit(function (e) {
                    e.preventDefault();
                    
                    const formData = {
                        userId: $('#userId').val(),
                        bookId: $('#bookId').val(),
                        borrowTime: $('#borrowTime').val(),
                        expectedReturnTime: $('#expectedReturnTime').val(),
                        status: 1 // 默认状态为借阅中
                    };

                    $.ajax({
                        url: '/borrow/api/borrow',
                        type: 'POST',
                        contentType: 'application/json',
                        data: JSON.stringify(formData),
                        success: function (res) {
                            if (res.code === 200) {
                                alert('添加成功！');
                                window.location.href = '/borrow/list';
                            } else {
                                alert('添加失败：' + res.message);
                            }
                        },
                        error: function () {
                            alert('服务器错误，请稍后再试！');
                        }
                    });
                });
            });

            // 格式化日期时间为datetime-local输入框所需的格式
            function formatDateTime(date) {
                return date.getFullYear() + '-' +
                    padZero(date.getMonth() + 1) + '-' +
                    padZero(date.getDate()) + 'T' +
                    padZero(date.getHours()) + ':' +
                    padZero(date.getMinutes());
            }

            // 补零函数
            function padZero(num) {
                return num.toString().padStart(2, '0');
            }
        </script>
    </section>
</body>

</html> 